<?php

namespace app\admin\model;

use app\common\enums\StatusEnum;
use think\Db;

class CustomerService extends \app\common\model\CustomerService
{
    /**
     * 列表
     * @param $where
     * @return array
     * @throws \think\exception\DbException
     */
    public static function getList($where)
    {
        $sql = static::field('id, name, phone, sort_num, status, create_time, update_time,code');

        if ($where) {
            $sql->where($where);
        }

        $data = $sql->paginate(null, false, ['query' => request()->param()]);

        $model = $data->each(function ($item, $key) {
            $item['create_time'] = get_datetime($item['create_time']);
            $item['update_time'] = get_datetime($item['update_time']);
            $item['statusText'] = StatusEnum::getStatus($item['status']);
            return $item;
        });

        // 获取总记录数
        $count = $model->total();
        return [
            'model' => $model,
            'count' => $count
        ];
    }

    /**
     * @param $ids
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getColumn($ids = [])
    {
        $sql = static::field('id, name')
            ->where(['status' => StatusEnum::ENABLED]);

        if ($ids) {
            $sql->where('id', 'in', $ids);
        }

        $model = $sql->select()->toArray();
        $result = [];
        foreach ($model as $key => $value) {
            $result[$value['id']] = $value['name'];
        }
        return $result;
    }

    /**
     * 删除
     * @param $id
     */
    public static function deleteOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->delete();
        }
    }

    public static function deleteAll($ids)
    {
        static::where('id', 'in', $ids)->delete();
    }

    /**
     * 修改状态
     * @param $id
     */
    public static function disabledOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->status = ($model->status == StatusEnum::ENABLED) ? StatusEnum::DISABLED : StatusEnum::ENABLED;
            $model->update_time = time();
            $model->save();
        }
    }

    /**
     * 添加修改
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function saveOne($data)
    {

        $saveData = [
            'name' => $data['name'],
            'phone' => $data['phone'],
            'uid' => $data['uid'],
            'qrcode' => $data['qrcode'],
            'code' => $data['code'],
            'sort_num' => (empty($data['sort_num']) || $data['sort_num'] < 0) ? 0 : $data['sort_num'],
            'status' => $data['status'],
            'update_time' => time()
        ];
        $oauthData = [];
        $oauthData['code'] = $data['code'];
        $oauthData['type'] = 1;
        if (isset($data['id']) && $data['id'] > 0) {
            $model = static::get($data['id']);
            if ($model) {
                if (!empty($model->code)|| !empty($model->uid)) {
                     unset($saveData['code']);
                     unset($saveData['uid']);
                } else {
                    Db::name('oauth')->where('id',$data['uid'])->data($oauthData)->update();
                }
            }

            Db::name('customer_service')
                ->where('id', $data['id'])
                ->data($saveData)
                ->update();

            $id = $data['id'];
        } else {
            $saveData['create_time'] = time();
            $id = Db::name('customer_service')->insertGetId($saveData);
            Db::name('oauth')->where('id',$data['uid'])->data($oauthData)->update();
        }
        return $id;
    }
    /**
     * 查看登录账号是否已绑定
     */
    public static function getUid($uid) {

        return  static::where('uid',$uid)->find();
    }
    /**
     * 绑定下级
     */
    public static  function addAgent($data) {
        $saveData = [];
        $msg = '';
        $status = true;
        //销售ID
        $model = static::get($data['id']);
        if ($model) {
            $agentInfo = DB::name('oauth')->where('phone',$data['phone'])->find();
            if ($agentInfo && empty($agentInfo['agent']) && empty($agentInfo['type']) ) {
                $saveData['code'] = substr($data['phone'],5,4).rand(10,99);
                $saveData['type'] = 2;
                $saveData['agent'] = $model['uid'];
                Db::name('oauth')->where('id',$agentInfo['id'])->data($saveData)->update();

            } else {
//                $msg  = '绑定信息不存在或者已被绑定';
                $status = false;
            }

        }
        return $status;
    }
}